﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using ESF.General;
using ESF.General.MVP;
using System.Web.UI.WebControls;

#endregion


namespace ESF.Web.Pages
{
	public class BaseEditPage<P, I, O> : BasePagePresenter <P> 
		where P : BaseEditPresenter<I, O>
		where I : IBaseEditViewer
	{ 
		#region Constantes
		
		public const string ITEMID = "ItemID";
		
		#endregion
	
	
		#region Propiedades
		
		public string ItemIDAsString
		{
			set
			{
				ViewState [ITEMID] = value;
			}
			get
			{
				if (ViewState [ITEMID] == null)
				{
					return null;
				}
				
				return ViewState [ITEMID] as string;
			}
		}
		
		
		public new int? ItemID
		{
			set
			{
				if (value == null)
				{
					ItemIDAsString = null;
					return;
				}
				
				ItemIDAsString = value.Value.ToString ();
			}
			get
			{
				return TextHelper.GetNullableInt (ItemIDAsString);
			}
		}
		
		#endregion
		
		
		#region Pre_Carga
		
		protected void Page_PreLoad (object sender, EventArgs e)
		{
			Button btnTemp;
			
		
			if (!IsPostBack)
			{
				ItemIDAsString = Request.QueryString [ITEMID];
			}
			
			
			btnTemp = GetSaveButton ();
			
			if (btnTemp != null)
			{
				btnTemp.UseSubmitBehavior = true;
				btnTemp.Click += btnSave_Click;
			}
			
			
			btnTemp = GetDeleteButton ();
			
			if (btnTemp != null)
			{
				btnTemp.UseSubmitBehavior = false;
				btnTemp.Click += btnDelete_Click;
			}
			
			
			btnTemp = GetClearButton ();
			
			if (btnTemp != null)
			{
				btnTemp.UseSubmitBehavior = false;
				btnTemp.Click += btnClear_Click;
			}
		}
		
		#endregion
		
		
		#region Botones
		
		protected virtual Button GetSaveButton () { return FindControl ("btnSave") as Button; }
		
		protected virtual Button GetDeleteButton () { return FindControl ("btnDelete") as Button; }
		
		protected virtual Button GetClearButton () { return FindControl ("btnClear") as Button; }
		
		#endregion
		
		
		#region Funciones
		
		protected void Save ()
		{
			_Presenter.Save ();
		}
		
		
		protected void Delete ()
		{
			_Presenter.Delete ();
		}
		
		
		protected void Clear ()
		{
			_Presenter.Clear ();
		}		
		
		#endregion
		
		
		#region Eventos
		
		protected void btnSave_Click(object sender, EventArgs e)
		{
			Save ();
		}
		
		
		protected void btnDelete_Click(object sender, EventArgs e)
		{
			Delete ();
		}
		
		
		protected void btnClear_Click(object sender, EventArgs e)
		{
			Clear ();
		}
		
		#endregion
	}
}
